import os
import socket

from django.http import HttpResponseRedirect, JsonResponse
from django.shortcuts import render

# Create your views here.
from django.urls import reverse
from rest_framework import permissions
from rest_framework.decorators import api_view
from rest_framework.routers import APIRootView

from .runner.reader import Reader

filedir = os.path.dirname(__file__)
ip = socket.gethostbyname(socket.gethostname())
par_file=filedir + f'/data/test.yaml'

class RootView(APIRootView):

    # 首页跳转
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

    def get(self, request, *args, **kwargs):
        # 首页跳转指定页面
        return HttpResponseRedirect(reverse('admin:index'))

@api_view(['GET'])
def testdata(request,*args,**kwargs):
    # 查看生成的测试数据文件使用的模板
    return render(request,template_name='testdata/data.html')

@api_view(['GET'])
def get_testjson(request,*args,**kwargs):
    # 根据测试数据文件变为树形结构
    data = Reader.read_casedata(f'{filedir}/runner/data/test.yaml')
    return JsonResponse({'testdata':data})